package com.example.lottery.controller;

import com.example.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.example.lottery.common.exception.ControllerException;
import com.example.lottery.common.pojo.CommonResult;
import com.example.lottery.common.utils.JacksonUtil;
import com.example.lottery.controller.param.ActivityCreateParam;
import com.example.lottery.controller.param.FindActivityListParam;
import com.example.lottery.controller.param.GetActivityDetailParam;
import com.example.lottery.controller.result.FindActivityListResult;
import com.example.lottery.controller.result.ActivityCreateResult;
import com.example.lottery.controller.result.GetActivityDetailResult;
import com.example.lottery.service.IActivityService;
import com.example.lottery.service.dto.ActivityCreateDTO;
import com.example.lottery.service.dto.ActivityDTO;
import com.example.lottery.service.dto.ActivityDetailDTO;
import com.example.lottery.service.dto.FindActivityDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private IActivityService activityService;
    @PostMapping("/create")
    public CommonResult<ActivityCreateResult> create(@RequestBody @Validated ActivityCreateParam activityCreateParam){
        log.info("create activityCreateParam：{}", JacksonUtil.writeValueAsString(activityCreateParam));
        ActivityCreateDTO activityCreateDTO=activityService.create(activityCreateParam);
        return CommonResult.success(convertToActivityCreateResult(activityCreateDTO));
    }

    private ActivityCreateResult convertToActivityCreateResult(ActivityCreateDTO activityCreateDTO) {
        if(activityCreateDTO==null){
            log.warn("活动创建失败");
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITY_CREATE_ERROR);
        }
        ActivityCreateResult activityCreateResult=new ActivityCreateResult();
        activityCreateResult.setActivityId(activityCreateDTO.getActivityId());
        return activityCreateResult;
    }
    @GetMapping("/find-list")
    public CommonResult<FindActivityListResult> findActivityList(@Validated FindActivityListParam findActivityParam){
        log.info("findActivityList FindActivityListParam:{}",findActivityParam);
        FindActivityDTO<ActivityDTO> findActivityDTO =activityService.findActivity(findActivityParam);
        return CommonResult.success(convertToFindActivityListResult(findActivityDTO));
    }

    private FindActivityListResult convertToFindActivityListResult(FindActivityDTO<ActivityDTO> findActivityDTO) {
        if(findActivityDTO==null){
            log.warn("活动列表查询失败");
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITY_FIND_ERROR);
        }
        FindActivityListResult activityListResult=new FindActivityListResult();
        activityListResult.setTotal(findActivityDTO.getTotal());
        activityListResult.setRecords(findActivityDTO.getRecords().stream()
                .map(activityDTO->{
                    FindActivityListResult.ActivityInfo activityInfo=new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setValid(activityDTO.valid());
                    activityInfo.setDescription(activityDTO.getDescription());
                    return activityInfo;
                }).collect(Collectors.toList()));
        return activityListResult;
    }
    @GetMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(@Validated GetActivityDetailParam getActivityDetailParam){
        log.info("getActivityDetail GetActivityDetailParam:{}",getActivityDetailParam);
        ActivityDetailDTO activityDetailDTO=activityService.findActivityDetail(getActivityDetailParam);
        return CommonResult.success(convertToGetActivityDetailResult(activityDetailDTO));
    }

    private GetActivityDetailResult convertToGetActivityDetailResult(ActivityDetailDTO activityDetailDTO) {
        if(activityDetailDTO==null){
            log.warn(ControllerErrorCodeConstants.ACTIVITY_DETAIL_FIND_ERROR.getMsg());
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITY_DETAIL_FIND_ERROR);
        }
        GetActivityDetailResult getActivityDetailResult=new GetActivityDetailResult();
        getActivityDetailResult.setActivityId(activityDetailDTO.getActivityId());
        getActivityDetailResult.setActivityName(activityDetailDTO.getActivityName());
        getActivityDetailResult.setDescription(activityDetailDTO.getDescription());
        getActivityDetailResult.setValid(activityDetailDTO.valid());

        getActivityDetailResult.setPrizes(activityDetailDTO.getActivityPrizeList().stream()
                .map(activityPrize->{
                    GetActivityDetailResult.PrizeResult prizeResult=new GetActivityDetailResult.PrizeResult();
                    prizeResult.setPrizeId(activityPrize.getPrizeId());
                    prizeResult.setPrizeAmount(activityPrize.getPrizeAmount());
                    prizeResult.setPrizeTierName(activityPrize.getPrizeTiers().getMsg());
                    prizeResult.setValid(activityPrize.valid());
                    prizeResult.setName(activityPrize.getName());
                    prizeResult.setDescription(activityPrize.getDescription());
                    prizeResult.setPrice(activityPrize.getPrice());
                    prizeResult.setImageUrl(activityPrize.getImageUrl());
                    return prizeResult;
                }).collect(Collectors.toList()));

        getActivityDetailResult.setUsers(activityDetailDTO.getActivityUserList().stream()
                .map(activityUser->{
                    GetActivityDetailResult.UserResult userResult=new GetActivityDetailResult.UserResult();
                    userResult.setUserId(activityUser.getUserId());
                    userResult.setUserName(activityUser.getUserName());
                    userResult.setValid(activityUser.valid());
                    return userResult;
                }).collect(Collectors.toList()));
        return getActivityDetailResult;
    }
}
